import {Component, Input, OnInit} from '@angular/core';
import {PaginationService} from '../pagination/pagination.service';
import {Table} from './table';
import {MessageService} from '../../../src/message/message.service';
import {timer} from 'rxjs';
import {getProperty} from '../../functions/core';


@Component({
  selector: 'ng-tools-table',
  templateUrl: './table.component.html',
  styleUrls: ['./table.component.scss']
})
export class TableComponent implements OnInit {
  @Input() pagination: PaginationService;
  @Input() table: Table;
  @Input() animate;
  tableData;
  displaySuffix = '_display';
  orderingParam = 'ordering';
  defaultAnimations = ['fadeInUp'];
  animation;
  isShowControl = false;
  onSort = null;

  constructor() {
  }

  ngOnInit() {
    if (this.animate === undefined) {
      this.animate = true;
    }
    if (this.animate) {
      this.animation = this.defaultAnimations[Math.floor(Math.random() * 100) % this.defaultAnimations.length];
    }
    this.pagination.dataLoadSubscriber$.subscribe(() => {
      this.tableData = this.pagination;
    });
    timer(500).subscribe(() => {
      if (this.table.options.buttons !== undefined) {
        this.table.options.buttons.forEach((item) => {
          if (item.isShow !== false) {
            this.isShowControl = true;
          }
        });
      }
    });
  }

  // noinspection JSMethodCanBeStatic
  get getProperty() {
    return getProperty;
  }

  sort(propName: string) {
    if (this.onSort !== propName && this.onSort !== '-' + propName) {
      this.onSort = propName;
      if (propName.indexOf(this.displaySuffix) !== -1) {
        this.pagination.queryParams = [{key: this.orderingParam, value: propName.replace(this.displaySuffix, ''), op: 's'}];
        this.pagination.reload();
      } else {
        this.pagination.queryParams = [{key: this.orderingParam, value: propName, op: 's'}];
        this.pagination.reload();
      }
    } else if (this.onSort === propName || this.onSort === '-' + propName) {
      if (this.onSort.indexOf('-') === -1) {
        propName = '-' + propName;
        this.onSort = propName;
        if (propName.indexOf(this.displaySuffix) !== -1) {
          this.pagination.queryParams = [{key: this.orderingParam, value: propName.replace(this.displaySuffix, ''), op: 's'}];
          this.pagination.reload();
        } else {
          this.pagination.queryParams = [{key: this.orderingParam, value: propName, op: 's'}];
          this.pagination.reload();
        }
      } else if (this.onSort.indexOf('-') !== -1) {
        propName = propName.replace('-', '');
        this.onSort = propName;
        if (propName.indexOf(this.displaySuffix) !== -1) {
          this.pagination.queryParams = [{key: this.orderingParam, value: propName.replace(this.displaySuffix, ''), op: 's'}];
          this.pagination.reload();
        } else {
          this.pagination.queryParams = [{key: this.orderingParam, value: propName, op: 's'}];
          this.pagination.reload();
        }
      }
    }
  }

  btnClicked(e, btnIndex, rowIndex) {
    const data = this.pagination.page.results[rowIndex];
    const func = this.table.options.buttons[btnIndex].click;
    e.stopPropagation();
    if (typeof func === 'function') {
      this.table.options.buttons[btnIndex].click(data, rowIndex);
    }
  }

  rowClicked(e, i) {
    e.stopPropagation();
    const data = this.pagination.page.results[i];
    const func = this.table.options.rowClick;
    if (typeof func === 'function') {
      this.table.options.rowClick(data, i);
    }
  }

}
